import axios, { type AxiosResponse } from "axios";

import { showFailToast } from "vant";

interface ResponseData<T> {
	code: number;
	data: T;
	message: string;
}

const service = axios.create({
	baseURL: "/wy",
	timeout: 50000,
});

// 添加请求拦截器
service.interceptors.request.use(config => {
	return config;
});

// 添加响应拦截器
service.interceptors.response.use(
	/* 约束一下response */
	async (response: AxiosResponse<any>) => {

		// 对响应数据做点什么
		const res = response.data;
		return res; /* 返回成功响应数据中的data属性数据 */
		// }
	},
	(error) => {
		// 对响应错误做点什么
		if (error.message.indexOf("timeout") != -1) {
			showFailToast("网络超时");
		} else if (error.message == "Network Error") {
			showFailToast("网络连接错误");
		} else {
			if (error.response.data) showFailToast(error.response.statusText);
			else showFailToast("接口路径找不到");
		}
		return Promise.reject(error);
	}
);

export default service;
